package com.codeyang.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeyang.domain.ProdProp;
import com.codeyang.domain.ProdPropValue;
import com.codeyang.service.ProdPropService;
import com.codeyang.service.ProdPropValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/25 20:27
 */
@Api("商品属性和规格管理")
@RestController
@RequestMapping("prod/spec")
public class ProdPropController {

    @Autowired
    private ProdPropService prodPropService;

    @Autowired
    private ProdPropValueService prodPropValueService;


    @ApiOperation("全查询商品属性规格")
    @GetMapping("list")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public ResponseEntity<List<ProdProp>> list() {
        List<ProdProp> prodPropList = prodPropService.list();
        return ResponseEntity.ok(prodPropList);
    }


    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    @ApiOperation("分页查询商品属性规格")
    public ResponseEntity<Page<ProdProp>> page(Page<ProdProp> page, ProdProp prodProp) {
        Page<ProdProp> prodPropPage = prodPropService.findProdPropPage(page, prodProp);
        return ResponseEntity.ok(prodPropPage);
    }

    @PostMapping
    @ApiOperation("新增一个属性的值")
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public ResponseEntity<Void> add(@RequestBody @Validated ProdProp prodProp) {
        this.prodPropService.save(prodProp);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("商品属性规格id查询属性值的集合")
    @GetMapping("listSpecValue/{id}")
    public ResponseEntity<List<ProdPropValue>> list(@PathVariable("id") Long propId) {
        List<ProdPropValue> prodPropValues = prodPropValueService.findPropValuesByPropId(propId);
        return ResponseEntity.ok(prodPropValues);
    }


    @GetMapping({"/listValueInfo"})
    @PreAuthorize("hasAuthority('prod:spec:info')")
    @ApiOperation("屬性的全查詢")
    public ResponseEntity<List<ProdProp>> listInfo() {
        List<ProdProp> list = this.prodPropService.list();
        return ResponseEntity.ok(list);
    }

//    @GetMapping({"/listSpecValue/{id}"})
//    @PreAuthorize("hasAuthority('prod:spec:info')")
//    @ApiOperation("属性值的回显")
//    public ResponseEntity<List<ProdPropValue>> listSpecValue(@PathVariable("id") Long id) {
//        List<ProdPropValue> prodPropValue = this.prodPropValueService.listSpecValue(id);
//        return ResponseEntity.ok(prodPropValue);
//    }


    @DeleteMapping({"/{id}"})
    @PreAuthorize("hasAuthority('prod:spec:info')")
    @ApiOperation("属性值的回显")
    public ResponseEntity<Void> listSpecValue(@PathVariable("id") Long id) {
        prodPropService.removeById(id);
        return ResponseEntity.ok().build();
    }


    @PutMapping
    @PreAuthorize("hasAuthority('prod:spec:update')")
    @ApiOperation("修改一个属性的值")
    public ResponseEntity<Void> update(@RequestBody @Validated ProdProp prodProp) {
        this.prodPropService.updateById(prodProp);
        return ResponseEntity.ok().build();
    }


}
